package spring.cloud.tasks.executor.listener.config;

import com.google.common.base.Strings;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.executor.node.TaskNodePaths;
import spring.cloud.tasks.executor.utils.zookeeper.Zookeeper;


@Slf4j
@Setter
@Getter
public class TaskConfiguration {
    private final String taskId;
    /**
     * 如果regCenter!=null, 取值从zk取
     */
    private Zookeeper zookeeper;
    private String taskType;
    private String taskClass;
    private boolean enabled = true;
    private String description = "";
    //
    /**
     * 作业启动时间的cron表达式.
     */
    private String cronExpression;
    /**
     * 作业暂停时间段，日期段。
     */
    private String pauseDatePeriod = "";
    /**
     * 作业暂停时间段，小时分钟段。
     */
    private String pauseTimePeriod = "";
    //
    /**
     * 作业分片总数.
     */
    private int taskItemTotalCount;
    /**
     * 分片序列号和个性化参数对照表.
     *
     * <p>
     * 分片序列号和参数用等号分隔, 多个键值对用逗号分隔. 类似map. 分片序列号从0开始, 不可大于或等于作业分片总数. 如: 0=a,1=b,2=c
     * </p>
     */
    private String taskItemParameters = "";
    /**
     * 作业自定义参数.
     *
     * <p>
     * 可以配置多个相同的作业, 但是用不同的参数作为不同的调度实例.
     * </p>
     */
    private String taskParameter = "";
    //
    /**
     * 每个作业的预分配列表
     */
    private String preferExecutorIdList = "";
    /**
     * 是否使用非preferList
     */
    private boolean useDisExecutorIdList = true;
    //
    /**
     * 每个分片的权重
     */
    private Integer loadLevel = 1;
    /**
     * 是否开启失效转移.
     */
    private boolean failover = true;
    /**
     * 默认不开启，只显示异常情况下的日志；异常情况的日志无论开闭都会显示；日志保存在zk，只保存最新100行；
     */
    private boolean showNormalLog = false;
    //
    /**
     * 统计作业处理数据数量的间隔时间.
     *
     * <p>
     * 单位: 秒. 只对处理数据流类型作业起作用.
     * </p>
     */
    private int processCountIntervalSeconds = 300;
    /**
     * Job超时时间
     */
    private int timeoutSeconds;

    public TaskConfiguration(String taskId, Zookeeper zookeeper) {
        this.taskId = taskId;
        this.zookeeper = zookeeper;
        reloadTaskConfiguration();
    }

    /**
     * update cron cache and return it
     */
    public String getCronExpressionFromZooKeeper() {
        cronExpression = zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.CRON));
        return cronExpression;
    }

    public boolean isDeleting() {
        return zookeeper.checkExists(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.TO_DELETE));
    }

    public void reloadTaskConfiguration() {
        if (zookeeper == null) {
            return;
        }
        //
        taskType = zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.JOB_TYPE));
        taskClass = zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.TASK_CLASS));
        enabled = Boolean.parseBoolean(zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.ENABLED)));
        description = zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.DESCRIPTION));
        //
        cronExpression = zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.CRON));
        pauseDatePeriod = zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.PAUSE_PERIOD_DATE));
        pauseTimePeriod = zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.PAUSE_PERIOD_TIME));
        //
        String taskItemTotalCountStr = zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.SHARDING_TOTAL_COUNT));
        taskItemTotalCount = Integer.parseInt(taskItemTotalCountStr);
        taskItemParameters = zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.SHARDING_ITEM_PARAMETERS));
        taskParameter = zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.JOB_PARAMETER));
        //
        String loadLevelStr = zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.LOAD_LEVEL));
        loadLevel = Integer.valueOf(loadLevelStr);
        failover = Boolean.parseBoolean(zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.FAILOVER)));
        showNormalLog = Boolean.parseBoolean(zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.SHOW_NORMAL_LOG)));
        //

        preferExecutorIdList = zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.PREFER_LIST));
        useDisExecutorIdList = Boolean.parseBoolean(zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.USE_DISPREFER_LIST)));
        //
        String processCountIntervalSecondsStr = zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.PROCESS_COUNT_INTERVAL_SECONDS));
        if (!Strings.isNullOrEmpty(processCountIntervalSecondsStr)) {
            processCountIntervalSeconds = Integer.parseInt(processCountIntervalSecondsStr);
        }
        String timeoutSecondsStr = zookeeper.getData(TaskNodePaths.getTaskNodePath(taskId, ConfigurationNode.TIMEOUTSECONDS));
        if (!Strings.isNullOrEmpty(timeoutSecondsStr)) {
            timeoutSeconds = Integer.parseInt(timeoutSecondsStr);
        }
    }


}